package com.koron.configure.shiftTip.service.impl;


import cn.hutool.core.util.StrUtil;
import com.koron.configure.shiftTip.bean.po.ConfigureShiftTip;
import com.koron.configure.shiftTip.mapper.ConfigureShiftTipMapper;
import com.koron.configure.shiftTip.service.ConfigureShiftTipService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 交接事项(ConfigureShiftTip)表服务实现类
 *
 * @author jzp
 * @since 2021-10-27 18:36:23
 */
@Service
public class ConfigureShiftTipServiceImpl implements ConfigureShiftTipService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ConfigureShiftTip getById(SessionFactory factory, String id) {
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ConfigureShiftTip> list(SessionFactory factory, ConfigureShiftTip configureShiftTip) {
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        return mapper.selectList(configureShiftTip);
    }

    @Override
    @TaskAnnotation("listByDockingId")
    public List<ConfigureShiftTip> listByDockingId(SessionFactory factory, String dockingId) {
        ConfigureShiftTip tip = new ConfigureShiftTip();
        tip.setDockingId(dockingId);
        List<ConfigureShiftTip> list = list(factory, tip);
        IntStream.range(0, list.size()).filter(i -> list.get(i).getSort() == null).forEach(i -> list.get(i).setSort(i+100));
        list.sort(Comparator.comparing(ConfigureShiftTip::getSort));
        return list;
    }

    @Override
    @TaskAnnotation("getMaxSort")
    public int getMaxSort(SessionFactory factory, String dockingId, String firstLevel, String secondLevel) {
        ConfigureShiftTip configureShiftTip = new ConfigureShiftTip();
        Assert.hasText(dockingId, "dockingId不能为空");
        configureShiftTip.setDockingId(dockingId);
        configureShiftTip.setFirstLevel(firstLevel);
        configureShiftTip.setSecondLevel(secondLevel);
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        Integer integer = mapper.selectMaxSort(configureShiftTip);
        return integer == null ? 0 : integer;
    }

    @Override
    @TaskAnnotation("getMergeMaxSort")
    public int getMergeMaxSort(SessionFactory factory, ConfigureShiftTip configureShiftTip) {
        int secondSort = getMaxSort(factory, configureShiftTip.getDockingId(), configureShiftTip.getFirstLevel(), configureShiftTip.getSecondLevel());
        if (secondSort > 0) {
            return secondSort;
        }
        int firstSort = getMaxSort(factory, configureShiftTip.getDockingId(), configureShiftTip.getFirstLevel(), null);
        if (firstSort > 0) {
            return firstSort;
        }
        return getMaxSort(factory, configureShiftTip.getDockingId(), null, null);
    }

    @Override
    @TaskAnnotation("setMaxSort")
    public void setMaxSort(SessionFactory factory, ConfigureShiftTip configureShiftTip) {
        if (configureShiftTip.getSort() != null) {
            return;
        }
        int maxSort = getMergeMaxSort(factory, configureShiftTip);
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        configureShiftTip.setSort(++maxSort);
        mapper.rollSort(configureShiftTip.getDockingId(), maxSort);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ConfigureShiftTip configureShiftTip) {
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        if (configureShiftTip.getSort() == null) {
            setMaxSort(factory, configureShiftTip);
        }

        // 如果是更新
        if (!StringUtils.isEmpty(configureShiftTip.getId())) {
            // 如果一级项有修改，相同的一级项全部进行修改
            ConfigureShiftTip source = getById(factory, configureShiftTip.getId());
            Assert.notNull(source, "根据id：" + configureShiftTip.getId() + "找不到配置");
            if (!source.getFirstLevel().equals(configureShiftTip.getFirstLevel())) {
                mapper.updateByFirstLevel(configureShiftTip.getDockingId(), source.getFirstLevel(), configureShiftTip.getFirstLevel());
            }
        }

        return mapper.insertOrUpdate(configureShiftTip) > 0;
    }

    @Override
    @TaskAnnotation("insert")
    public boolean insert(SessionFactory factory, ConfigureShiftTip configureShiftTip) {
        Assert.notNull(configureShiftTip.getSort(), "插入时排序号不能为空");
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        mapper.rollSort(configureShiftTip.getDockingId(), configureShiftTip.getSort());
        return mapper.insertOrUpdate(configureShiftTip) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<ConfigureShiftTip> tips) {
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        return mapper.insertOrUpdateBatch(tips) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ConfigureShiftTip configureShiftTip) {
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        return mapper.update(configureShiftTip) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        ConfigureShiftTipMapper mapper = factory.getMapper(ConfigureShiftTipMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 通过dockingId删除数据
     */
    @Override
    @TaskAnnotation("removeByDockingId")
    public boolean removeByDockingId(SessionFactory factory, String dockingId) {
        if (StrUtil.isEmpty(dockingId)) {
            return false;
        }
        ConfigureShiftTip configureShiftTip = new ConfigureShiftTip();
        configureShiftTip.setDockingId(dockingId);
        List<ConfigureShiftTip> list = list(factory, configureShiftTip);

        return deleteBatchIds(factory, list.stream().map(ConfigureShiftTip::getId).collect(Collectors.toList()));
    }

}
